package com.g001.util;

import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;

public class CachedResourceLoader extends ResourceLoader {
	
	private static CachedResourceLoader instance;
	
	private Map<String,Image> resources = new HashMap<String,Image>();
	
	public static Font fontBig = null;
	public static Font font = null;
	public static Font fontSmall = null;

	static {
		try {
			fontBig = new AngelCodeFont("data/g001/ericBig.fnt","data/g001/ericBig_00.png");
			font = new AngelCodeFont("data/g001/eric.fnt","data/g001/eric_00.png");
			fontSmall = new AngelCodeFont("data/g001/ericSmall.fnt","data/g001/ericSmall_00.png");
		} catch (SlickException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * singleton
	 */
	private CachedResourceLoader(){
		super();
	}
	
	public static CachedResourceLoader getInstance(){
		if(instance == null) instance = new CachedResourceLoader();
		return instance; 
	}
	
	
	/**
	 * method to get a resource Image
	 * @param path
	 * @return
	 */
	public Image getImage(String path){
		if(resources.get(path)!=null) {
			System.out.println("Get the cached version");
			return resources.get(path);
		} else {
			long time = System.currentTimeMillis();
			System.out.print("read from disk ..."+System.currentTimeMillis());
			Image image = null;
			try {
				image = new Image(path);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("took "+(System.currentTimeMillis()-time)+" ms");
			return image;
		}
	}

}
